<?php
class AdvsController extends AppController {

	var $name = 'Advs';
	var $helpers = array('Html', 'Form');
    
    function my_advs()
    {
        if($this->Session->read('User.type')=='employer')
        {
            $this->set('choose1',43);
            $this->Adv->recursive=-1;            
            $all = $this->Adv->find('all',array('conditions'=>array('Adv.employer_id'=>$this->Session->read('User.id')),'order'=>'Adv.created DESC'));
            $end = $this->Adv->find('all',array('conditions'=>array('Adv.employer_id'=>$this->Session->read('User.id'),'Adv.status'=>1,'Adv.end_date < '=>date("Y-m-d")),'order'=>'Adv.created DESC'));
            $post = $this->Adv->find('all',array('conditions'=>array('Adv.employer_id'=>$this->Session->read('User.id'),'Adv.status'=>1,'Adv.end_date >= '=>date("Y-m-d")),'order'=>'Adv.created DESC'));
            $wait = $this->Adv->find('all',array('conditions'=>array('Adv.employer_id'=>$this->Session->read('User.id'),'Adv.status'=>0),'order'=>'Adv.created DESC'));
            
            
            $this->set('all',$all);        
            $this->set('end',$end);        
            $this->set('post',$post);        
            $this->set('wait',$wait);        
        }
        else
        { 
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true); 
        }    
    }
    
    function post()
    {
        if($this->Session->read('User.type')=='employer')
        {
            $this->set('choose1',43);
            if(!empty($this->data))
            {
                if($this->data['Adv']['image']['type']=='image/gif' || $this->data['Adv']['image']['type']=='image/jpeg' || $this->data['Adv']['image']['type']=='image/pjpeg' || $this->data['Adv']['image']['type']=='image/png')
                {
                    $size = getimagesize($this->data['Adv']['image']['tmp_name']);
                    if($size[0]<=250 && $size[1]<=100)
                    {
                        $name = substr($this->data['Adv']['image']['name'],0,strpos($this->data['Adv']['image']['name'],'.'));
                        $type = substr($this->data['Adv']['image']['name'],strpos($this->data['Adv']['image']['name'],'.'),5); 
                        $temp = $this->data['Adv']['image'];
                        
                            $this->data['Adv']['image']='';                        
                            $this->data['Adv']['employer_id']=$this->Session->read('User.id');
                            $this->Adv->create();
                            $this->Adv->save($this->data);                
                            $newid = $this->Adv->id;
                            
                        $this->data['Adv']['id']=$newid;
                        $this->data['Adv']['image']=$temp;
                            
                        $destination = ADV_DIRECTORY.$this->data['Adv']['id'].$type;
                        if( move_uploaded_file($this->data['Adv']['image']['tmp_name'],$destination)  )
                        {
                            $this->data['Adv']['image']=$destination;
                            $this->Adv->save($this->data);
                            $this->Session->setFlash(__('You Adv has been created', true));
                            $this->redirect(array('controller'=>'advs', 'action'=>'my_advs'),null, true); 
                        }
                        else
                        { $this->Session->setFlash(__('The banner is failed to be uploaded, please try again', true));}
                
                    }
                    else
                    { $this->Session->setFlash(__('Maximum size is 250 x 100 pixel', true)); }
                }
                else
                { $this->Session->setFlash(__('Please provide the banner with image file (.jpg, .jpeg. gif, .png)', true)); }  
            }                                      
        }
        else
        { 
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true); 
        }    
    }
 
     function repost($id = null)
    {
        if($this->Session->read('User.type')=='employer')
        {
            $this->Adv->recursive=-1;
            $data = $this->Adv->find('first',array('conditions'=>array('Adv.id'=>$id, 'Adv.employer_id'=>$this->Session->read('User.id')) ));
            if(empty($data))
            {
                $this->Session->setFlash(__('Invalid Link', true)); 
                $this->redirect(array('controller'=>'advs', 'action'=>'my_advs'),null, true);
            }
            else
            {
                if( $data['Adv']['status']==1 && $data['Adv']['end_date'] < date("Y-m-d") )
                {
                    $temp['Adv']['id'] = $data['Adv']['id'];
                    $temp['Adv']['status'] = 0;
                    $temp['Adv']['slip'] = '';
                    $temp['Adv']['created'] = date("Y-m-d");
                    $temp['Adv']['end_date'] = '';
                    $this->Adv->save($temp);
                    
                    $this->Session->setFlash(__('Your Adv has been repost', true)); 
                    $this->redirect(array('controller'=>'advs', 'action'=>'my_advs'),null, true);
                }
                else
                {
                    $this->Session->setFlash(__("This Adv isn't activate yet or still posted", true)); 
                    $this->redirect(array('controller'=>'advs', 'action'=>'my_advs'),null, true);
                }
            }
        }
        else
        { 
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true);        
        }    
    } 
 
    function edit( $id = null )
    {
        if($this->Session->read('User.type')=='employer')
        {
            $this->set('choose1',43);
            if(!empty($this->data))
            {
                if($this->data['Adv']['image']['name']!='')
                {
                    if($this->data['Adv']['image']['type']=='image/gif' || $this->data['Adv']['image']['type']=='image/jpeg' || $this->data['Adv']['image']['type']=='image/pjpeg' || $this->data['Adv']['image']['type']=='image/png')
                    {
                        $size = getimagesize($this->data['Adv']['image']['tmp_name']);
                        if($size[0]<=250 && $size[1]<=100)
                        {
                            // hapus file sebelumnya
                            $this->Adv->recursive=-1;
                            $data = $this->Adv->read(null,$this->data['Adv']['id']);
                            if( file_exists($data['Adv']['image']) && $data['Adv']['image']!='' && $data['Adv']['image']!='n/a' )
                            {
                                unlink($data['Adv']['image']);
                            }

                            $name = substr($this->data['Adv']['image']['name'],0,strpos($this->data['Adv']['image']['name'],'.'));
                            $type = substr($this->data['Adv']['image']['name'],strpos($this->data['Adv']['image']['name'],'.'),5); 
                            $destination = ADV_DIRECTORY.$this->data['Adv']['id'].$type;
                            if( move_uploaded_file($this->data['Adv']['image']['tmp_name'],$destination)  )
                            {
                                $this->data['Adv']['image']=$destination;
                                $this->Adv->save($this->data);
                                $this->Session->setFlash(__('Your Adv has been edited', true));
                                $this->redirect(array('controller'=>'advs', 'action'=>'my_advs'),null, true); 
                            }
                            else
                            { $this->Session->setFlash(__('The banner is failed to be uploaded, please try again', true));}
                        }
                        else
                        { $this->Session->setFlash(__('Maximum size is 250 x 100 pixel', true)); }
                    }
                    else
                    { $this->Session->setFlash(__('Please provide the banner with image file (.jpg, .jpeg. gif, .png)', true)); }  
                }
                else
                {
                    unset($this->data['Adv']['image']);
                    $this->Adv->save($this->data);
                    $this->redirect(array('controller'=>'advs', 'action'=>'my_advs'),null, true);
                }
            }
            
            
            //if(empty($this->data))
            //{
                $this->data = $this->Adv->find('first',array('conditions'=>array('Adv.id'=>$id,'Adv.employer_id'=>$this->Session->read('User.id'),'Adv.status'=>0)));
                if(empty($this->data))
                { 
                    $this->Session->setFlash(__('Invalid ID', true)); 
                    $this->redirect(array('controller'=>'advs', 'action'=>'my_advs'),null, true);
                }
            //}
            
        }
        else
        { 
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true); 
        }    
    }
    
    function payment($id = null)
    {
        if($this->Session->read('User.type')=='employer')
        {        
        $this->set('choose1',43);
            if(!empty($this->data))
            {
                if($this->data['Adv']['slip']['type']=='image/gif' || $this->data['Adv']['slip']['type']=='image/jpeg' || $this->data['Adv']['slip']['type']=='image/pjpeg' || $this->data['Adv']['slip']['type']=='image/png')
                {

                    $this->Adv->recursive=-1;                    
                    $data = $this->Adv->read(null,$this->data['Adv']['id']);
                    if( file_exists($data['Adv']['slip']) && $data['Adv']['slip']!='' && $data['Adv']['slip']!='n/a' )
                    {
                        unlink($data['Adv']['slip']);
                    }        
                            
                    $name = substr($this->data['Adv']['slip']['name'],0,strpos($this->data['Adv']['slip']['name'],'.'));
                    $type = substr($this->data['Adv']['slip']['name'],strpos($this->data['Adv']['slip']['name'],'.'),5); 
                    $destination = ADV_SLIP_DIRECTORY.$this->data['Adv']['id'].$type;
                    if( move_uploaded_file($this->data['Adv']['slip']['tmp_name'],$destination)  )
                    {
                        $this->data['Adv']['slip']=$destination;
                        $this->Adv->save($this->data);
                        $this->Session->setFlash(__('Thank you for your payment', true));
                        $this->redirect(array('controller'=>'advs', 'action'=>'my_advs'),null, true); 
                    }
                    else
                    { $this->Session->setFlash(__('Upload failed, please try again', true)); }

                }
                else
                { $this->Session->setFlash(__('Please provide image file', true)); }         
            }
            $this->Adv->recursive=-1;
            $this->data = $this->Adv->find('first',array(  'conditions'=>array('Adv.id'=>$id,'Adv.employer_id'=>$this->Session->read('User.id'),'Adv.status'=>0  )));
            if(empty($this->data))
            { 
                $this->Session->setFlash(__('Invalid ID', true));
                $this->redirect(array('controller'=>'advs', 'action'=>'my_advs'),null, true);
            }
        }
        else
        {
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true); 
        }               
    }  

    function delete($id = null) 
    {
        if($this->Session->read('User.type')=='employer')
        {
                $query = "SELECT * FROM `advs` Adv WHERE employer_id=".$this->Session->read('User.id')." AND id = ".$id." AND ( status = 0 OR ( status = 1 AND end_date > '".date("Y-m-d")."' ) )";  
                $data = $this->Adv->query($query);
                if(empty($data))
                { 
                    $this->Session->setFlash(__('Invalid Link', true)); 
                    $this->redirect(array('controller'=>'advs', 'action'=>'my_advs'),null, true);
                }
                else
                {
                    if( file_exists($data[0]['Adv']['image']) && $data[0]['Adv']['image']!='' && $data[0]['Adv']['image']!='n/a' )
                    {
                        unlink($data[0]['Adv']['image']);
                    }
                    
                    if( file_exists($data[0]['Adv']['slip']) && $data[0]['Adv']['slip']!='' && $data[0]['Adv']['slip']!='n/a' )
                    {
                        unlink($data[0]['Adv']['slip']);
                    }                   
                    
                    $this->Adv->del($id);

                    $this->Session->setFlash(__('Your Adv has been deleted', true));
                }
                $this->redirect(array('controller'=>'advs', 'action'=>'my_advs'),null, true); 
        }
        else
        { 
            $this->Session->setFlash(__('Please login first', true));
            $this->redirect(array('controller'=>'employers', 'action'=>'login'),null, true); 
        }    
    }
    
    function adv_list()
    {
        $this->Adv->recursive=-1;
        $advs = $this->Adv->find('all',array('conditions'=>array('Adv.status'=>'1', 'Adv.end_date >='=>date("Y-m-d") )));

        $jum_advs = ADV_PER_PAGE;
        $jum = count($advs);
        if($jum!=0)
        {
            if($jum > $jum_advs)
            {
                $i=0; $kembar = false;
                do
                {
                    $kembar = false;
                    $arr[$i]=rand(0,$jum-1);
                    for($j=0;$j<$i;$j++)
                    {
                        if($arr[$j]==$arr[$i])
                        { $kembar = true; $j=$i; }
                    }
                    if(!$kembar)
                    { $i++; }
                }
                while ($i < $jum_advs );
            }
            else
            {
                for($i=0;$i<$jum;$i++)
                { $arr[$i]=$i; }
            }
            
            for($i=0;$i<count($arr);$i++)
            { $indeks = $arr[$i]; $result[$i] = $advs[$indeks]; }
            return $result; 
        }
        else
        { return false; }
        
    }
    
    
	function index() 
    {
		$this->Adv->recursive = -1;
		$advs = $this->Adv->find('all',array('conditions'=>array('Adv.status'=>1,'Adv.end_date >='=>date("Y-m-d")),'order'=>'Adv.created'));
        $this->set('advs', $advs);
	}

    function index_admin()  
    {
        if($this->Session->read('User.type')=='admin')
        {
            $this->layout='ajax';
        }
        else
        {
            $this->Session->setFlash('You are forbidden for this page');
            $this->redirect(array('controller'=>'jobs', 'action'=>'login_admin'),null, true);
        }             
    }

    function filter_admin()  
    {
        $this->layout='ajax';
        if(!empty($this->data['status']))
        {
            $this->Adv->recursive=0;
            $query = "SELECT advs.*, employers.company_name FROM `advs`,`employers` WHERE employers.id = advs.employer_id ";

            if($this->data['status']==2)
            { $query.= " AND advs.status = 0 AND advs.slip <> ''"; }
            if($this->data['status']==3)
            { $query.= " AND advs.status = 1 AND advs.end_date >= '".date("Y-m-d")."'"; }
            if($this->data['status']==4)
            { $query.= " AND advs.status = 1 AND advs.end_date < '".date("Y-m-d")."'"; }
            $mydate = date("Y-m-d", mktime(0, 0, 0, date('m')-6, date('d'), date('Y')));
            if($this->data['status']==5)
            { $query.= " AND advs.status = 1 AND advs.end_date < '".$mydate."'"; }
            if($this->data['status']==6)
            { $query.= " AND advs.status = 0 AND advs.slip = '' "; }
            if($this->data['status']==7)
            { $query.= " AND advs.status = 0 AND Advs.slip = ''  AND advs.created < '".$mydate."'"; }
            $query.= " ORDER BY advs.id ";
            $advs = $this->Adv->query($query);  
            $this->set('advs',$advs);
        }
        
        if(empty($this->data))
        {
            $query = "SELECT advs.*, employers.company_name FROM `advs`,`employers` WHERE employers.id = advs.employer_id ";
            $query.= " ORDER BY advs.id ";
            $advs = $this->Adv->query($query);
            $this->set('advs',$advs);
        }
    }

    function slip_admin( $id = null )
    { 
        $this->layout = 'ajax';
        $this->Adv->recursive=-1;        
        $data = $this->Adv->read(null,$id);
        $this->set('data',$data);   
    
    }
    
    function approve_admin( $id = null )
    { 
        $mydate = date("Y-m-d", mktime(0, 0, 0, date('m')+1, date('d'), date('Y')));
        $this->data['Adv']['id']=$id;
        $this->data['Adv']['status']=1;
        $this->data['Adv']['created']=date("Y-m-d");
        $this->data['Adv']['end_date']=$mydate;
        $this->Adv->save($this->data);
        $this->redirect(array('controller'=>'advs','action'=>'filter_admin'));
    }
    
    function edit_admin( $id = null )
    {
        if(!empty($this->data))
        {
            $this->Adv->save($this->data);
            $this->redirect(array('controller'=>'advs','action'=>'filter_admin'));    
        }
        else
        {
            $this->layout = 'ajax';
            $this->Adv->recursive=-1;
            $this->data = $this->Adv->read(null,$id);
        }
    }
    
    function delete_admin($id)
    {  
        if($this->Session->read('User.type')=='admin')
        {
            $this->layout = 'ajax';
            $this->Adv->del($id);
            $this->redirect(array('controller'=>'advs','action'=>'filter_admin'));
        }        
    } 
	
}
?>